/**
 * Created with Intellij IDEA.
 * Description:
 * User: wangj
 * Date: 2022-02-26
 * Time: 21:46
 */
import java.util.Random;
import java.util.Scanner;

public class Game {
    //     定义12行12列，实际扫雷的区域只有10*10
    static int ROW = 12;

    static int COL = 12;
    //    定义雷的个数
    static int NUM = 20;
    //    0为无炸弹，1为有炸弹
    public static byte BOOM = '1';

    public Game(int ROW, int COL, int NUM) {
        this.ROW = ROW;
        this.COL = COL;
        this.NUM = NUM;
    }

    public static void setMines(int[][] mine_board, int row, int col) {
        int i = 0;
        while (i < NUM) {
            Random random = new Random();//默认随机种子是系统时间
            int x = random.nextInt(ROW - 2) + 1;
            int y = random.nextInt(COL - 2) + 1;
            if (mine_board[x][y] == BOOM) {
                continue;
            }
            mine_board[x][y] = BOOM;
            i++;
        }
    }

    public static void showBoard(int[][] board, int row, int col) {
        System.out.print(" ");
        for (int i = 1; i <= row - 2; i++) {
            System.out.printf("%4d", i);
        }
        System.out.println();
        System.out.print(" ");
        for (int k = 1; k <= col - 2; k++) {
            System.out.print("----");
        }
        System.out.println();
        for (int i = 1; i <= ROW - 2; i++) {
            System.out.printf("%2d|", i);
            for (int j = 1; j <= COL - 2; j++) {
                System.out.printf(" %c |", board[i][j]);
            }
            System.out.println();
            for (int k = 1; k <= COL - 2; k++) {
                System.out.print("----");
            }
            System.out.println();
        }
    }

    public static void flag(int[][] board, int a, int b) {
        if (b == 0) {
            System.out.print("请输入插旗坐标<x y> ");
            Scanner sc = new Scanner(System.in);
            int x = sc.nextInt();
            int y = sc.nextInt();
            board[x][y] = '$';
        } else {
            while (true) {
                System.out.print("请输入取消插旗坐标<x y> ");
                Scanner sc = new Scanner(System.in);
                int i = sc.nextInt();
                int j = sc.nextInt();
                if (board[i][j] == '$') {
                    board[i][j] = '*';
                    break;
                } else {
                    System.out.println("输入坐标有误！");
                    continue;

                }
            }
        }
    }

    public static int countMines(int[][] board, int x, int y) {
        return board[x - 1][y - 1] + board[x - 1][y]
                + board[x - 1][y + 1] + board[x][y - 1]
                + board[x][y + 1] + board[x + 1][y - 1]
                + board[x + 1][y] + board[x + 1][y + 1] - 8 * '0';
    }

    public static void game() {
        int[][] show_board = new int[ROW][COL];
        int[][] mine_board = new int[ROW][COL];
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COL; j++) {
                show_board[i][j] = '*';
                mine_board[i][j] = '0';
            }
        }
        setMines(mine_board, ROW, COL);
        int tot = (ROW - 2) * (COL - 2) - NUM;
        int clear = 0;
        while (true) {
            showBoard(show_board, ROW, COL);
            System.out.print("请输扫雷入坐标<x y> ");
            Scanner sc = new Scanner(System.in);
            int x = sc.nextInt();
            int y = sc.nextInt();
            if ((x == 0 && y == 0) || (x == 0 && y == 1)) {
                flag(show_board, x, y);
                continue;
            }
            if (!(x >= 1 && x <= ROW - 2 && y >= 1 && y <= COL - 2)) {
                System.out.println("扫雷的位置有问题！");
                continue;
            }
            if (show_board[x][y] != '*') {
                System.out.println("扫雷的位置上已经排除！");
                continue;
            }
            if (mine_board[x][y] == '1') {
                System.out.println("不好意思，你被炸死了！");
                break;
            } else {
                int count = countMines(mine_board, x, y);
                show_board[x][y] = count + '0';
                clear++;
            }
            if (clear >= tot) {
                System.out.println("恭喜你，游戏胜利！");
                break;
            }
        }

    }

}
